<?php

namespace Common\Model;

/**
 * 订单支付金额变化记录表
 * @modify trainyao 2016-01-14 创建
 * @index orderrecord_id(`orderrecord_id`)
 * @index hotelpay_id(`hotelpay_id`, `type`)
 */
Class OrderPayRecordModel extends BaseModel {

    protected $trueTableName = 'tb_order_pay_record';
    private $redisInstance = null;
    protected function _before_update(&$data, $options) {}
    /**
     * 根据订单操作记录查询支付金额变化
     * @modify trainyao 2016-01-14      创建
     * @param  mixin   $orderRecordIds 订单操作记录id(s)
     * @param  array   $type           需要取的类型
     * @return array                   支付金额变信息
     */
    public function getInfoByOrderRecord($orderRecordIds, $type = null) {
        $option = array();

        if (is_array($orderRecordIds)) {
            $orderRecordIds = implode(',', $orderRecordIds);
        }
        if (strpos($orderRecordIds, ',') !== false) {
            $option['orderrecord_id'] = array('in', $orderRecordIds);
        } else {
            $option['orderrecord_id'] = $orderRecordIds;
        }

        if (!empty($type)) {
            $option['type'] = array('in', $type);
        }

        $data = $this->where($option)->select();
        return empty($data) ? array() : $data;
    }

    /**
     * 根据客栈id取数据
     * @modify trainyao 2016-01-19 创建
     * @param  integer   $hotelId   客栈id
     * @return array              支付方式记录数据
     */
    public function getInfoByHotel($hotelId, $startDate = null, $endDate = null) {
        $option = array(
            'hotelentity_id' => $hotelId,
        );
        $stringOption = array();
        if (!empty($startDate)) {
            array_push($stringOption, 'createtime >= "' . $startDate . '"');
        }
        if (!empty($endDate)) {
            array_push($stringOption, 'createtime <= "' . $endDate . '"');
        }
        if (!empty($stringOption)) {
            $stringOption = implode(' and ', $stringOption);
            $data = $this->where($option)->where($stringOption)->order('id desc')->select();
        } else {
            $data = $this->where($option)->select();
        }

        return empty($data) ? array() : $data;
    }

    /**
     * 返回查询
     * 返回查询客栈日期支付明细数据
     * @modify songanwei 2016-01-18
     * @param integer $hotelId 客栈ID
     * @param date $startDate 查询日期
     * @param integer $type 类型
     */
    public function getInfosByHotelDate($hotelId, $date, $type = null) {
        $where['hotelentity_id'] = $hotelId;
        $where['date(createtime)'] = $date;
        if (!empty($type)) {
            $where['type'] = $type;
        }
        return $this->where($where)->select();
    }

    /**
     * @param $hotelId
     * @param $startDate
     * @param $endDate
     * @param $otherType  是否获取其他消费
     * @return mixed
     */
    public function getInfosBetweenHotelDate($hotelId, $startDate, $endDate, $otherType = false) {
        $where['hotelentity_id'] = $hotelId;
        $where['createtime'] = ['between',[$startDate, $endDate]];
        if ($otherType !== false) {
            $where['type'] = array('not in', [0,1,2,3]);
            $where['isdelete'] = NOT_DELETE;
        }
        return $this->where($where)->select();
    }
    /**
     * 返回查询
     * 返回订单支付明细数据
     * @modify Paddy 2016-11-25
     */
    public function getInfosByOrderId($orderinfo_id) {
        $where['orderinfo_id'] = $orderinfo_id;
        $where['type'] = array('not in', [0,1,2,3]);
        $where['isdelete'] = NOT_DELETE;

        return $this->where($where)->select();
    }

    /**
     * 返回查询
     * 返回订单支付明细数据
     * @modify Paddy 2016-11-25
     */
    public function getInfosByOrderinfoId($orderinfo_id) {
        $where['orderinfo_id'] = $orderinfo_id;
        return $this->field('id,hotelpay_id,pay,type')->where($where)->select();
    }

    /**
     * 增加多条记录
     * @modify trainyao 2016-01-15 创建
     * @param  array   $infos     记录数据
     * @return  boolean  执行结果
     */
    public function addInfo($infos) {
        return $this->addAll($infos) == false ? false : true;
    }

    /**
     * 更改订单记录
     * @modify trainyao 2016-05-19 创建
     * @param  integer   $id        条目id
     * @param  integer   $hotelPayId   客栈收款支付方式对应tb_hotel_pay.id
     * @param  integer   $type   类型0=收款 1= 押金 2=退款 3=退押金
     * @param  integer   $pay   金额变化值
     * @param  integer   $vipUserId   vipUserId
     * @param  integer   $transBeforePay   转化前金额
     * @param  integer   $currencyId   货币id
     * @return boolean              执行结果
     */
    public function saveInfo($id, $hotelPayId = null, $type = null, $pay = null, $vipUserId = null, $transBeforePay = null, $currencyId = null) {
        $option = array(
            'id' => $id,
        );

        $data['id'] = $id;
        if (!is_null($hotelPayId)) {
            $data['hotelpay_id'] = $hotelPayId;
        }
        if (!is_null($type)) {
            $data['type'] = $type;
        }
        if (!is_null($pay)) {
            $data['pay'] = $pay;
        }
        if (!is_null($vipUserId)) {
            $data['vipuser_id'] = $vipUserId;
        }
        if (!is_null($transBeforePay)) {
            $data['trans_before_pay'] = $transBeforePay;
        }
        if (!is_null($currencyId)) {
            $data['currency_id'] = $currencyId;
        }

        return $this->where($option)->save($data) === false ? false : true;
    }

    /**
     * 根据订单操作记录查询支付金额变化
     * @modify wenhui 2017-10-11      创建
     * @param  mixin   $id 订单支付记录id(s)
     */
    public function getOrderPayRecordById($id) {
        $option = array();

        if (is_array($id)) {
            $id = implode(',', $id);
        }
        if (strpos($id, ',') !== false) {
            $option['id'] = array('in', $id);
            $data = $this->where($option)->select();
        } else {
            $option['id'] = $id;
            $data = $this->where($option)->find();
        }

        return empty($data) ? array() : $data;
    }

    /**
     * 删除 其他消费
     * wenhui 2017-10-11      创建
     * @param  integer   $hotelId     客栈id
     * @param  integer   $orderId   父订单iz
     */
    public function deleteInfo($hotelId, $orderId) {
        $option = array(
            'hotelentity_id' => $hotelId,
            'orderinfo_id' => $orderId,
            'type' => array('not in', [0,1,2,3]),
        );
        $data['isdelete'] = IS_DELETE;

        return $this->where($option)->save($data) === false ? false : true;
    }

    /**
     * 返回查询
     * @modify  wenhui 2017-10-11      创建
     */
    public function getInfosByHotelAndPayId($hotelId, $hotelPayId) {

        $option['hotelentity_id'] = $hotelId;
        $option['hotelpay_id'] = $hotelPayId;

        return $this->where($option)->find();
    }

    /**
     * @modify wenhui 2018-01-16    创建
     */
    public function getInfoByOrderIdAnPayId($orderId, $hotelPayId) {
        $where['orderinfo_id'] = $orderId;
        $where['hotelpay_id'] = $hotelPayId;

        return $this->field('id,hotelpay_id,pay,type')->where($where)->select();
    }
}
